
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LogWindow.java
 *
 * Created on Mar 7, 2012, 4:56:52 PM
 */
/**
 *
 * @author Anselmus Krisma A K
 */
public class LogWindow extends javax.swing.JDialog implements Runnable {

    Thread mMainThread;
    BufferedInputStream mReader;

    /** Creates new form LogWindow */
    public LogWindow(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();

        File tFile = new File("./Output.txt");
        if (tFile.exists()) {
            try {
                mReader = new BufferedInputStream(new FileInputStream(tFile));
            } catch (FileNotFoundException ex) {
                System.out.println(this.getClass().getName() + " log file not found");
            }
        }

        mMainThread = new Thread(this);
        mMainThread.start();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        logText = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Log Absensi PTI");

        logText.setColumns(20);
        logText.setEditable(false);
        logText.setRows(5);
        logText.setEnabled(false);
        jScrollPane1.setViewportView(logText);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 530, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea logText;
    // End of variables declaration//GEN-END:variables

    @Override
    public void run() {
        StringBuilder tBuilder = new StringBuilder();
        while (true) {
            try {
                if (mReader.available() > 0) {
                    tBuilder.append((char) mReader.read());
                    logText.setText(tBuilder.toString());
                } else {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException ex) {
                        System.out.println(this.getClass().getName() + " error sleep");
                    }
                }
            } catch (IOException ex) {
                System.out.println(this.getClass().getName() + " error read");
            }
        }
    }
}
